package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Robot;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * 机器人疑似封号回调
 *
 * <pre>
 * {
 *     "nType": 1007,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcRelationSerialNo": "",
 *         "nSex": 1,
 *         "vcNickName": "",
 *         "vcBase64NickName": "",
 *         "vcHeadImgUrl": "",
 *         "vcCountry": "",
 *         "vcProvince": "",
 *         "vcCity": "",
 *         "vcBase64WhatsUp": "",
 *         "nChatRoomCount": "",
 *         "vcReason": "",
 *         "dtDateTime": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1007Handler implements CallbackHandler {

    @Resource
    private RobotMapper robotMapper;

    @Override
    public String name() {
        return "机器人疑似封号回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1007;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        LambdaUpdateWrapper<Robot> wrapper = new LambdaUpdateWrapper<Robot>()
                .set(Robot::getSealStatus, 20)
                .set(Robot::getUpdatedTime, LocalDateTime.ofInstant(data.getInstant("dtDateTime"), ZoneId.systemDefault()))
                .eq(Robot::getId, callback.getRobotId());
        robotMapper.update(wrapper);
    }
}
